package leetcode.editor.cn;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Offer2_007 {
    List<List<Integer>> result = new ArrayList();
    public List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);
        int len = nums.length;
        for(int i = 0; i <=len - 3; i++){
            if(i > 0 && nums[i] == nums[i-1]) continue;
            for(int j = i+1; j <= len -2; j++){
                if(j > i+1 && nums[j] == nums[j-1]) continue;
                int k = len - 1;
                int target = -(nums[i]+nums[j]);
                while(k > j){
                    if(nums[k] > target) k--;
                    else if(nums[k] < target) break;
                    else {
                        result.add(Arrays.asList(nums[i],nums[j],nums[k]));
                        k--;
                    }
                }
            }
        }
        return result;
    }

    public static void main(String[] args) {
        List<List<Integer>> lists = new Offer2_007().threeSum(new int[]{0, 0, 0,0});
    }
}
